package com.itmk.web.companyInfo.controller;

import com.itmk.utils.JWTUtils;
import com.itmk.utils.Result;
import com.itmk.utils.UID;
import com.itmk.web.companyInfo.entity.AddResume;
import com.itmk.web.companyInfo.entity.Company;
import com.itmk.web.companyInfo.entity.JobInfo;
import com.itmk.web.companyInfo.service.CompanyService;
import com.itmk.web.userInfo.entity.BasicInfo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.batch.BatchProperties;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    /**
     * 获取公司信息
     * @param request
     * @return
     */
    @RequestMapping("/getCompanyInfo")
    @ResponseBody
    public Result getCompanyInfo(HttpServletRequest request){
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        Company company = companyService.getCompanyInfo(uuid);
        return Result.ok().put("data",company);
    }

    /**
     * 修改公司logo
     * @param request
     * @param pic
     * @return
     */
    @RequestMapping("/updateCompanyLogo")
    @ResponseBody
    public Result updateCompanyLogo(HttpServletRequest request, MultipartFile pic){

        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        if(pic!=null){
            //file重命名
            String originalFilename = pic.getOriginalFilename();
            String ext = "."+originalFilename.split("\\.")[1];
            String imageUUID = UUID.randomUUID().toString().replace("-","");
            String fileName = imageUUID+ext;
            //上传图片
//            ApplicationHome applicationHome = new ApplicationHome(this.getClass());
//            String pre = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath()+
//                    "\\src\\main\\resources\\static\\images\\companyLogo\\";
            String property = System.getProperty("user.dir")+"\\itmk-base-web";
            String pre = property + "\\target\\classes\\static\\images\\companyLogo/";

            companyService.updateLogo(fileName,uuid);
            String path1 = pre+fileName;
            String path = path1.replace("/","\\");
            try {
                pic.transferTo(new File(path));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return Result.ok("修改成功").put("logo",fileName);
        }
        return Result.error("上传图片失败");
    }

    /**
     * 更新公司信息
     * @param request
     * @param company
     * @return
     */
    @RequestMapping("/updateCompanyInfo")
    @ResponseBody
    public Result updateCompanyInfo(HttpServletRequest request,@RequestBody Company company){
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        Boolean result = companyService.updateCompanyInfo(company,uuid);
        if(result){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }

    /**
     * 添加岗位
     * @param request
     * @param jobInfo
     * @return
     */
    @RequestMapping("/addJobInfo")
    @ResponseBody
    public Result addJobInfo(HttpServletRequest request, @RequestBody JobInfo jobInfo){
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        String job_uuid = UID.getUUID();
        jobInfo.setJob_uuid(job_uuid);
        Boolean res = companyService.addJobInfo(jobInfo,uuid);
        if(res){
            return Result.ok("成功添加岗位");
        }
        return Result.error("添加岗位失败");
    }

    /**
     * 根据job_uuid删除岗位
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/deleteJobInfo/{job_uuid}")
    @ResponseBody
    public Result deleteJobInfo(HttpServletRequest request,@PathVariable String job_uuid){
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        boolean res = companyService.deleteJobInfo(job_uuid,uuid);
        if(res){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }


    /**
     * 获取企业所有的岗位
     * @param request
     * @return
     */
    @RequestMapping("/getCompanyJob")
    @ResponseBody
    public Result allJobByCompany(HttpServletRequest request){
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        List<JobInfo> res = companyService.allCompanyJob(uuid);
        return Result.ok().put("data",res);
    }
    /**
     * 根据job_uuid获取一条job信息
     */
    @RequestMapping("/getOneJob/{job_uuid}")
    @ResponseBody
    public Result getOnJob(HttpServletRequest request,@PathVariable String job_uuid) {
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        JobInfo jobInfo = companyService.getOneJob(job_uuid, uuid);
        if(jobInfo!=null){
            return Result.ok().put("data", jobInfo);
        }
        return Result.error("获取数据失败");
    }
    /**
     * 根据skill来获取job信息
     */
    @RequestMapping("/getJobBySkill/{skill}")
    @ResponseBody
    public Result getJobBySkill(HttpServletRequest request,@PathVariable String skill) {
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        List<JobInfo> list = companyService.getJobBySkill(skill,uuid);
        if(list!=null){
            return Result.ok().put("data", list);
        }
        return Result.error("获取数据失败");
    }
    /**
     * 修改job信息
     */

    @RequestMapping("/updateJobInfo")
    @ResponseBody
    public Result updateJobInfo(HttpServletRequest request, @RequestBody JobInfo jobInfo) {
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        boolean res = companyService.updateJobInfo(jobInfo,uuid);
        if(res){
            return Result.ok("修改数据成功");
        }
        return Result.error("获取数据失败");
    }
    /**
     * 岗位被投简历
     */
    @RequestMapping("/addResume")
    @ResponseBody
    public Result addResume(HttpServletRequest request, @RequestBody AddResume addResume) {
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        boolean res = companyService.addResume(addResume);
        if(res){
            return Result.ok("修改数据成功");
        }
        return Result.error("获取数据失败");
    }

    /**
     * 获取投递岗位的所有简历信息
     */
    @RequestMapping("/getAllResume/{job_uuid}")
    @ResponseBody
    public Result getAllResume(HttpServletRequest request,@PathVariable String job_uuid){
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        List<String> resumes = companyService.getAllResume(job_uuid);
        if(resumes!=null){
            return Result.ok().put("data",resumes);
        }
        return Result.error("当前岗位无用户投递");
    }
}
